package model

import (
	"gorm.io/gorm"
	"share-chat/global"
	"time"
)

type Share struct {
	Id        uint32         `gorm:"column:id;type:int UNSIGNED;comment:分享ID;primaryKey;not null;" form:"id" json:"id"`                       // 分享ID
	UserId    int32          `gorm:"column:user_id;type:int;comment:用户ID:发布人;not null;" form:"user_id" json:"user_id"`                     // 用户ID:发布人
	Title     string         `gorm:"column:title;type:varchar(60);comment:标题;not null;" form:"title" json:"title"`                            // 标题
	Content   string         `gorm:"column:content;type:varchar(300);comment:内容:长度不得超过300字符;not null;" form:"content" json:"content"` // 内容:长度不得超过300字符
	Image     string         `gorm:"column:image;type:varchar(500);default:NULL;" form:"image" json:"image"`
	Video     string         `gorm:"column:video;type:varchar(500);default:NULL;" form:"video" json:"video"`
	Status    int32          `gorm:"column:status;type:int;comment:用户ID:发布人;not null;" form:"status" json:"status"`                                             // 状态 1:待审核 2:审核通过 3:下架
	CreatedAt time.Time      `gorm:"column:created_at;type:datetime(3);comment:发布时间;not null;default:CURRENT_TIMESTAMP(3);" form:"created_at" json:"created_at"` // 发布时间
	UpdatedAt time.Time      `gorm:"column:updated_at;type:datetime(3);comment:修改时间;not null;default:CURRENT_TIMESTAMP(3);" form:"updated_at" json:"updated_at"` // 修改时间
	DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" form:"deleted_at" json:"deleted_at"`
}

func (s *Share) TableName() string {
	return "share"
}

func (s *Share) AddShare() error {
	return global.DB.Create(&s).Error
}

func (s *Share) UpdatedShare() error {
	return global.DB.Updates(&s).Error
}

func (s *Share) ListShare() (share []Share, err error) {
	err = global.DB.Where("status = 2").Find(&s).Error
	return
}
